package com.aacar.op.web.mem;

import com.aacar.op.entity.mem.Member;
import com.aacar.op.entity.user.User;
import com.aacar.op.service.mem.MemberService;
import com.aacar.op.utils.Constant;
import com.aacar.op.utils.DateUtil;
import com.aacar.op.utils.enumeration.MenuPermissions;
import com.aacar.op.utils.pojo.ActionResult;
import com.aacar.op.utils.pojo.datatables.DataTablesResult;
import com.aacar.op.utils.web.PageUtils;
import com.aacar.op.utils.web.Servlets;
import com.aacar.op.web.user.UserController;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.Map;

/**
 * 注册用户管理
 * @author Chao
 *
 */
@Controller
@RequestMapping("/op/mem")
public class MemberController {
	
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private MemberService memService;
    
    @RequestMapping(method = {RequestMethod.GET})
    public String list() {
        return "op/mem/mem";
    }
    
    /**
     * datatables json
     */
    @RequestMapping(value = "datatables", method = {RequestMethod.GET, RequestMethod.POST})
    public
    @ResponseBody
    DataTablesResult<Member> datatables(HttpServletRequest request,
                                      @RequestParam(value = "search_startDate", required = false)
                                      @DateTimeFormat(pattern = Constant.DATE_PATTERN_SHORT) Date startDate,
                                      @RequestParam(value = "search_endDate", required = false)
                                      @DateTimeFormat(pattern = Constant.DATE_PATTERN_SHORT) Date endDate) {

        Pageable pageable = PageUtils.pageableFromDateTables(request);
        //使用Map传参到Service层,便于扩展.
        //获取"search_"的参数传as Map.
        Map<String, Object> params = Servlets.getParametersStartingWith(request, "search_");
        startDate = DateUtil.safeStartDate(startDate);
        endDate = DateUtil.safeEndDate(endDate);
        params.put("startDate", startDate);
        params.put("endDate", endDate);
        PageInfo<Member> pageInfo = memService.getPage(params, pageable);
        return DataTablesResult.build(pageInfo);
    }
    
    
    /**
     * update :: POST
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    public
    @ResponseBody
    ActionResult update(@Valid @ModelAttribute("entity") Member entity, BindingResult result) {

        if (result.hasErrors()) {
            logger.warn(result.toString());
            return ActionResult.failure("数据不合法,保存失败。");
        }
        
        ActionResult actionResult;
        try {
            memService.saveOrUpdate(entity);
            actionResult = ActionResult.success("用户&quot;" + entity.getUserName() + "&quot;保存成功。");
        } catch (Exception e) {
            logger.error("用户保存异常:{}", e);
            actionResult = ActionResult.failure("用户&quot;" + entity.getUserName() + "&quot;保存失败。");
        }
        return actionResult;
    }
    

    /**
     * 跳转详情页面:: GET
     */
    @RequestMapping(value = "detail", method = RequestMethod.GET)
    public String input(@RequestParam Long id, Model model) {
    	model.addAttribute("mem", memService.get(id));
        return "op/mem/mem-detail";
    }


    /**
     * 跳转充值页面:: GET
     */
    @RequestMapping(value = "recharge", method = RequestMethod.GET)
    public String recharge(@ModelAttribute("entity") Member entity, Model model) {
        model.addAttribute("menuPerSelect2JsonArr", MenuPermissions.asSelect2JsonArr());
        return "op/mem/mem-recharge";
    }
}
